$(document).ready(function(){
	var desk_name=$.session.get('diningTableName');
	if(desk_name==""||desk_name==null){
		if(confirm('你还没有开台，是否开台？')){
			window.location.href = "chashier_desk.html";
			return false;
		}
	}
	var foods_data=new Array();
	var i=0;
	var sum_price;
//计数器
	function counter(){
		$("body").on('change','.input-num',function(){
			var num=parseInt($(this).val());
			if(num<0||num==0||isNaN(num)){
			num=1;
			}
			var index=$(this).parents('.food-list').index()-1;
			foods_data[index].food_num=num;
			show_foods_data2();
		});
		$("body").on('click','.btn-add',function(){
			var num=parseInt($(this).parent().prev().val());
			num=num+1;
			var index=$(this).parents('.food-list').index()-1;
			foods_data[index].food_num=num;
			show_foods_data2();
		});
		$("body").on('click','.btn-minus',function(){
			var num=parseInt($(this).parent().next().val());
			num=num-1;
			if(num<0||num==0){
				num=1;
			}
			var index=$(this).parents('.food-list').index()-1;
			foods_data[index].food_num=num;
			show_foods_data2();
		});
	}
//	导航搜索
	$('.nav-second table tr td a').click(function(){
		var condition=$(this).text();
		$.ajax({
			url:'dish_sreachDataByKind.action',
			type:'POST',
			dataType:'json',
			data:{
				'enumName':condition
			},
			success:function(data){
				if (data.status == "200") {
					$('.swiper-slide').remove();
					for (var i = 0; i <data.data.length; i++) {
						var value = data.data[i];
						$('.swiper-wrapper').append("<div class='swiper-slide'><div class='order'><table><tr><td class='order-position'>"+
									"<img src='"+value.dishiHeadImgUrl+"'/></td>"+
									"<td class='order-name'>"+value.name+"</td>"+
									"<td class='order-price'>￥"+value.price+"</td>"+
									"<td><button class='btn btn-warning addto-order' id='addto-order'>添加</button></td></tr></table></div></div>"					
						);
					}

			}
				else{
//					window.location.href = "error.html";
				}
			}
		}).done(function(){
			var swiper = new Swiper('.order-right .swiper-container', {
		        pagination: '.swiper-pagination',
		        slidesPerView: 4,
		        paginationClickable: true,
		        spaceBetween: 30,
		        freeMode: true,
		        direction: 'vertical'
		    });
	});
	});
//	搜索
	$('.search').click(function(){
		if($('.input-search').val()==''){
			getFood();
			return ;
		}
		$.ajax({
			url:'dish_sreachDataByShortFor.action',
			type:'POST',
			dataType:'json',
			data:{
				'shortFor':$('.input-search').val(),
			},
		success:function(data){
			if (data.status == "200") {
				$('.swiper-slide').remove();
				for (var i = 0; i <data.data.length; i++) {
					var value = data.data[i];
					$('.swiper-wrapper').append("<div class='swiper-slide'><div class='order'><table><tr><td class='order-position'>"+
								"<img src='"+value.dishiHeadImgUrl+"'/></td>"+
								"<td class='order-name'>"+value.name+"</td>"+
								"<td class='order-price'>￥"+value.price+"</td>"+
								"<td><button class='btn btn-warning addto-order' id='addto-order'>添加</button></td></tr></table></div></div>"					
					);
				}

		}
			else{
//				window.location.href = "error.html";
			}
			
		}
		}).done(function(){
			var swiper = new Swiper('.order-right .swiper-container', {
		        pagination: '.swiper-pagination',
		        slidesPerView: 4,
		        paginationClickable: true,
		        spaceBetween: 30,
		        freeMode: true,
		        direction: 'vertical'
		    });
	});
	});
//	封装获取所有菜的信息
	function getFood(){
		$.ajax({
			url:'dish_getDataList.action',
			type:'POST',
			dataType: 'json',
			success: function(data){
				if (data.status == "200") {
					$(".swiper-slide").remove();
					for (var i = 0; i <data.data.length; i++) {
						var value = data.data[i];
						$('.swiper-wrapper').append(
								"<div class='swiper-slide'><div class='order'><table><tr><td class='order-position'>"+
									"<img src='"+value.dishiHeadImgUrl+"'/></td>"+
									"<td class='order-name'>"+value.name+"</td>"+
									"<td class='order-price'>￥"+value.price+"</td>"+
									"<td><button class='btn btn-warning addto-order' id='addto-order'>添加</button></td></tr></table></div></div>"					
						);
					}
					

			}
				else{
					window.location.href = "error.html";
				}
		},
		}).done(function(){
			var swiper = new Swiper('.order-right .swiper-container', {
		        pagination: '.swiper-pagination',
		        slidesPerView: 4,
		        paginationClickable: true,
		        spaceBetween: 30,
		        freeMode: true,
		        direction: 'vertical'
		    });
		 });
	}
//	获取菜的信息
	$.ajax({
		url:'dish_getDataList.action',
		type:'POST',
		dataType: 'json',
		success: function(data){
			if (data.status == "200") {
				$(".swiper-slide").empty("");
				for (var i = 0; i <data.data.length; i++) {
					var value = data.data[i];
					$('.swiper-wrapper').append(
							"<div class='swiper-slide'><div class='order'><table><tr><td class='order-position'>"+
								"<img src='"+value.dishiHeadImgUrl+"'/></td>"+
								"<td class='order-name' data-foodid='"+value.id+"'>"+value.name+"</td>"+
								"<td class='order-price'>￥"+value.price+"</td>"+
								"<td><button class='btn btn-warning addto-order' id='addto-order'>添加</button></td></tr></table></div></div>"					
					);
				}
				

		}
			else{
				window.location.href = "error.html";
			}
	},
	}).done(function(){
		var swiper = new Swiper('.order-right .swiper-container', {
	        pagination: '.swiper-pagination',
	        slidesPerView: 4,
	        paginationClickable: true,
	        spaceBetween: 30,
	        freeMode: true,
	        direction: 'vertical'
	    });

//添加菜到订单数组

		function addto_foods_data(){
			$('.addto-order').click(function(){
				var food_name=$(this).parent().parent().children().eq(1).text();
				var string_price=$(this).parent().parent().children().eq(2).text();
				var food_price=string_price.substr(1,string_price.length);
				var food_num=1;
				var dishId=$(this).parent().prev().prev().attr('data-foodid');
				var obj={'dishId':dishId,'dishName':food_name,'price':food_price,'food_num':food_num};
				if(find_food(food_name)){
					
				}
				else{
				foods_data[i]=obj;
				i++;
				}
				show_foods_data();
//查看菜是否已存在
			function find_food(food_name){
				for(var j=0;j<foods_data.length;j++){
					if(food_name==foods_data[j].dishName){
						foods_data[j].food_num=foods_data[j].food_num+1;
						return true;
					}
				}
			};
		});
		};

			//订单显示
		
		function show_foods_data(){
			var sum=0;
			$('.food-list').remove();
			for(var i=0;i<foods_data.length;i++){
				var price=foods_data[i].price*foods_data[i].food_num;
				sum=sum+price;
				sum_price=sum;
				var foods=$('#food_sum').before("<li class='list-group-item order-left-content food-list'>"+
						"<table style='width: 100%;'>"+
							"<tr style='width: 100%;'>"+
								"<td class='order-left-name'>"+foods_data[i].dishName+"</td>"+
								"<td class='order-left-num'>"+
									"<div class='container-fluid'>"+
										"<div class='row'>"+
											"<div class='input-group'>"+
												"<span class='input-group-btn'>"+
													"<button class='btn btn-default btn-minus' type='button'>-</button>"+
												"</span>"+
												"<input type='text' class='form-control input-num' value='"+foods_data[i].food_num+"' style='text-align: center;'>"+
												"<span class='input-group-btn'>"+
													"<button type='button' class='btn btn-default btn-add'>+</button></span></div></div></div></td>"+
								"<td class='order-left-price'>￥"+price+"</td><td class='order-left-del'><button class='btn btn-warning delete-food-list' type='button'>删除</button></td></tr></table></li>");
			};
			$('#order-sum-price').html('￥'+sum);
			}
		addto_foods_data();
		counter();
		
	});		
//获取到订单
	function show_foods_data2(){
		var sum=0;
		$('.food-list').remove();
		for(var i=0;i<foods_data.length;i++){
			var price=foods_data[i].price*foods_data[i].food_num;
			sum=sum+price;
			sum_price=sum;
			var foods=$('#food_sum').before("<li class='list-group-item order-left-content food-list'>"+
					"<table style='width: 100%;'>"+
						"<tr style='width: 100%;'>"+
							"<td class='order-left-name'>"+foods_data[i].dishName+"</td>"+
							"<td class='order-left-num'>"+
								"<div class='container-fluid'>"+
									"<div class='row'>"+
										"<div class='input-group'>"+
											"<span class='input-group-btn'>"+
												"<button class='btn btn-default btn-minus' type='button'>-</button>"+
											"</span>"+
											"<input type='text' class='form-control input-num' value='"+foods_data[i].food_num+"' style='text-align: center;'>"+
											"<span class='input-group-btn'>"+
												"<button type='button' class='btn btn-default btn-add'>+</button></span></div></div></div></td>"+
							"<td class='order-left-price'>￥"+price+"</td><td class='order-left-del'><button class='btn btn-warning delete-food-list' type='button'>删除</button></td></tr></table></li>");
		};
		$('#order-sum-price').html('￥'+sum);
		addto_foods_data();
		counter();
		};
		//从订单中删除
		$("body").on('click','.delete-food-list',function(){
			var index=$(this).parents('.food-list').index()-1;
			foods_data.splice(index,1);
			i--;
			show_foods_data2();
		});
//		订单提交
		$('.commit').click(function(){
			if(desk_name==''||desk_name==null){
				alert('你必须先开台！');
				window.location.href = "chashier_desk.html";
			}else{

				$.ajax({
					url:'dishOrder_orderDish.action',
					type:'POST',
					dataType:'json',
					data:{
						'diningTableID':$.session.get('diningTableID'),
						'diningTableName':$.session.get('diningTableName'),
						'price':sum_price,
						'tid':11,
						'tname':'111',
					    'creatUid':$.session.get('creatUid'),
						'creatUname':$.session.get('creatUname'),
						'itemList':JSON.stringify(foods_data)
					},
					success:function(data){
						if (data.status == "200") {

							window.location.href = "chashier_order_message1.html";
					} else {
							window.location.href = "error.html";
						}
					}
				})
			}

		});

});
